package cn.gqr.controller;

import cn.gqr.entity.BookOrderDetail;
import cn.gqr.entity.Order;
import cn.gqr.entity.UserOrder;
import cn.gqr.entity.UserOrderVO;
import cn.gqr.service.OrderService;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author gqr
 * @company null
 * @create 2022-06-12 17:10
 */
@RestController
@RequestMapping("/api/adm")
public class OrderAdminController {

    @Autowired
    private OrderService orderService;

    //根据订单id添加快递单号
    @PreAuthorize("hasAnyAuthority('adm:addCourierNumber')")
    @RequestMapping("/order/addCourierNumber")
    public Map<String,Object> insertCourierNumberByOrderId(@RequestBody Order order){
        String courierNumber = order.getCourierNumber();
        if(courierNumber!=null && courierNumber!=""){
            boolean b = orderService.insertCourierNumberByOrderId(courierNumber, order.getId());
            return RespJson.pack(200,b?"添加成功":"添加失败",null);
        }
        return RespJson.pack(200,"快递单号不能为空",null);
    }


    //获取所有买家的订单信息和用户信息并且分页,可动态条件查询
    @PreAuthorize("hasAnyAuthority('adm:findOrderUserByPage')")
    @RequestMapping("/order/findOrderUserByPage/{start}/{num}")
    public Map<String,Object> selectOrderHeaderAndUser(@PathVariable("start") int start, @PathVariable("num") int num, UserOrderVO userOrderVO){
        Map<String, Object> map = orderService.selectOrderHeaderAndUser(userOrderVO, start, num);
        return RespJson.pack(200,"订单信息",map);
    }


    //根据订单id,查找订单明细的所有对应书本信息
    @PreAuthorize("hasAnyAuthority('adm:findOrderDetail')")
    @RequestMapping("/order/findOrderDetail/{id}")
    public Map<String,Object> selectBookOrderDetailsByOrderId(@PathVariable("id") Integer orderId){
        List<BookOrderDetail> bookOrderDetails = orderService.selectBookOrderDetailsByOrderId(orderId);
        return  RespJson.pack(200,"订单明细",bookOrderDetails);
    }



}
